<?php

declare(strict_types=1);

namespace App\Entity;

use App\Models\CleanerOrderDailyStatistic;

class CleanerOrderDailyStatisticEntity extends ABaseEntity
{
    public function __construct(CleanerOrderDailyStatistic $model)
    {
        $this->model = $model;
    }

    /**
     * 保洁员做单数加一
     *
     * @param $cleanerId
     * @param $date
     */
    public static function incr(int $cleanerId, string $date)
    {
        $entity = self::getByCleanerIdAndDate($cleanerId, $date);
        if ($entity !== null) {
            $count = $entity->getCount() + 1;
            $entity->setCount($count);

            return;
        }

        $entity = self::getNewInstance();
        $entity->setCleanerId($cleanerId);
        $entity->setDate($date);
        $entity->setCount(1);
        $entity->save();
    }

    /**
     * @return CleanerOrderDailyStatisticEntity|null
     */
    public static function getByCleanerIdAndDate(int $cleanerId, string $date)
    {
        $model = CleanerOrderDailyStatistic::where('cleaner_id', $cleanerId)
            ->where('date', $date)
            ->first();

        if ($model === null) {
            return null;
        }

        return new self($model);
    }

    public function getCleanerId(): int
    {
        return (int) $this->model->cleaner_id;
    }

    public function setCleanerId(int $cleaner_id): void
    {
        $this->model->cleaner_id = $cleaner_id;
    }

    public function getCleanerName(): string
    {
        return (string) $this->model->cleaner_name;
    }

    public function setCleanerName(string $cleaner_name): void
    {
        $this->model->cleaner_name = $cleaner_name;
    }

    public function getCount(): int
    {
        return $this->model->count;
    }

    public function setCount(int $count): void
    {
        $this->model->count = $count;
    }

    public function getDate(): string
    {
        return $this->model->date;
    }

    public function setDate(string $date): void
    {
        $this->model->date = $date;
    }
}
